package zgrjb.snbq.teach_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zgrjb.snbq.teach_platform.dto.model.ExamDO;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.dto.req.AddExamRequest;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.mapper.ExamMapper;
import zgrjb.snbq.teach_platform.service.ExamService;
import zgrjb.snbq.teach_platform.util.IDUtil;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @Author: breathe
 * @CreateTime: 2025-05-21
 */
@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Override
    public JsonData add(AddExamRequest request) {
        User user = LoginInterceptor.getCurrentUser();
        ExamDO examDO = new ExamDO();
        examDO.setId((Long) IDUtil.geneSnowFlakeID());
        examDO.setName(request.getName());
        examDO.setDescription(request.getDescription());
        examDO.setType(request.getType());
        examDO.setUrl(request.getUrl());
        examDO.setContent(request.getContent());
        if (request.getBegin() != null) {
            examDO.setBegin(LocalDateTime.ofInstant(request.getBegin().toInstant(), ZoneId.systemDefault()));
        }
        if (request.getEnd() != null) {
            examDO.setEnd(LocalDateTime.ofInstant(request.getEnd().toInstant(), ZoneId.systemDefault()));
        }
        examDO.setAns(request.getAns());
        examDO.setClassId(request.getClassId());
        examMapper.insert(examDO);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData list(Long classId) {
        List<ExamDO> list = examMapper.selectList(new QueryWrapper<ExamDO>()
                .eq("class_id", classId));
        return JsonData.buildSuccess(list);
    }
}
